﻿using BaiTu.TestIng.Domain;
using ServiceStack;
using ServiceStack.Messaging.Redis;
using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BaiTu.ServiceStack.Testing
{
    class Program
    {
        static void Main(string[] args)
        {
            var serverAppHost = new ServerAppHost();
            serverAppHost.Init();
            serverAppHost.Start("http://localhost:1400/");
            Console.WriteLine("Server running.  Press enter to terminate...");
            Console.ReadLine(); //Block the server from exiting (i.e. if running inside Console App)
        }
    }

    public class ServerAppHost : AppHostHttpListenerBase
    {
        public ServerAppHost() : base("Test Server", typeof(HelloService).Assembly) { }

        public override void Configure(Funq.Container container)
        {
            var redisFactory = new PooledRedisClientManager("192.168.1.104:6379");
            container.Register<IRedisClientsManager>(redisFactory);

            var mqHost = new RedisMqServer(redisFactory, retryCount: 2);
            mqHost.RegisterHandler<Hello>(this.ServiceController.ExecuteMessage);

            mqHost.Start(); //Starts listening for messages
        }

    }

    public class HelloService : Service
    {
        public object Any(Hello req)
        {
            FF.WriteLog(2, req.Name);
            return new HelloResponse()
            {
                Result = "Helloguozhiqi," + req.Name
            };
        }
    }

}
